package com.chuantou.yuba.ui.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.TrendsImageTO;
import com.chuantou.yuba.ui.GalleryActivity;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.TrendsImageView;
import com.chuantou.yuba.util.UiUtil;

public class NineGridlayout extends ViewGroup {
	/**
	 * 图片之间的间隔
	 */
	private int mGap = 5;
	private int mColumns;
	private int mRows;
	private List<TrendsImageTO> mImageList;
	private int mTotalWidth;
	private ViewRecycler<TrendsImageView> mViewRecycler;

	public NineGridlayout(Context context) {
		super(context);
		setupViews(context);
	}

	public NineGridlayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		setupViews(context);
	}

	private void setupViews(Context context) {
		mTotalWidth = UiUtil.getScreenWidth(context) - UiUtil.dip2px(context, 60);
	}

	public void setViewRecyler(ViewRecycler<TrendsImageView> recycler) {
		this.mViewRecycler = recycler;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {

	}

	private void layoutChildrenView() {
		int childrenCount = mImageList.size();

		int singleWidth = (mTotalWidth - mGap * (3 - 1)) / 3;
		int singleHeight = singleWidth;

		// 根据子view数量确定高度
		ViewGroup.LayoutParams params = getLayoutParams();
		params.height = singleHeight * mRows + mGap * (mRows - 1);
		setLayoutParams(params);

		for (int i = 0; i < childrenCount; i++) {
			TrendsImageView childrenView = (TrendsImageView) getChildAt(i);
			TrendsImageTO imageTO = mImageList.get(i);
			BitmapBinder.bindNetImage(getContext(), childrenView, imageTO.getImage(), R.drawable.default_image_small, false);
			int[] position = findPosition(i);
			int left = (singleWidth + mGap) * position[1];
			int top = (singleHeight + mGap) * position[0];
			int right = left + singleWidth;
			int bottom = top + singleHeight;
			childrenView.layout(left, top, right, bottom);
		}

	}

	private int[] findPosition(int childNum) {
		int[] position = new int[2];
		for (int i = 0; i < mRows; i++) {
			for (int j = 0; j < mColumns; j++) {
				if ((i * mColumns + j) == childNum) {
					position[0] = i;// 行
					position[1] = j;// 列
					break;
				}
			}
		}
		return position;
	}

	public int getGap() {
		return mGap;
	}

	public void setGap(int gap) {
		this.mGap = gap;
	}

	public void setImages(List<TrendsImageTO> lists) {
		mImageList = lists;
		if (mViewRecycler == null) {
			mViewRecycler = new ViewRecycler<TrendsImageView>();
		}
		mViewRecycler.fillScrapViews(this);
		removeAllViews();
		if (mImageList == null || mImageList.isEmpty())
			return;
		// 初始化布局
		generateChildrenLayout(lists.size());
		int i = 0;
		while (i < lists.size()) {
			TrendsImageView iv = getItemImageView(i);
			addViewInLayout(iv, i, generateDefaultLayoutParams(), true);
			i++;
		}
		layoutChildrenView();
	}

	/**
	 * 根据图片个数确定行列数量 对应关系如下 num row column 1 1 1 2 1 2 3 1 3 4 2 2 5 2 3 6 2 3 7
	 * 3 3 8 3 3 9 3 3
	 * 
	 * @param length
	 */
	private void generateChildrenLayout(int length) {
		if (length <= 3) {
			mRows = 1;
			mColumns = length;
		} else if (length <= 6) {
			mRows = 2;
			mColumns = 3;
			if (length == 4) {
				mColumns = 2;
			}
		} else {
			mRows = 3;
			mColumns = 3;
		}
	}

	private OnClickListener mItemClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Object obj = v.getTag();
			if (obj != null) {
				String tagString = String.valueOf(obj);
				if (TextUtils.isDigitsOnly(tagString)) {
					int index = Integer.parseInt(tagString);
					ArrayList<String> paths = new ArrayList<String>();
					for (TrendsImageTO image : mImageList) {
						paths.add(image.getImage());
					}
					GalleryActivity.start(getContext(), paths, index);
				}
			}
		}
	};

	private TrendsImageView getItemImageView(int index) {
		TrendsImageView iv = mViewRecycler.getScrapView();
		if (iv == null) {
			iv = new TrendsImageView(getContext());
			iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
		}
		iv.setOnClickListener(mItemClickListener);
		iv.setTag(index);
		iv.setDefaultImageResId(R.drawable.default_image_small);
		iv.setErrorImageResId(R.drawable.default_image_small);
		return iv;
	}

}